﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SupermaketManager.Core;
using SupermarketManager.Model;
using SupermaketManager.DataAccess;
using System.Data.SqlClient;

namespace SupermaketManager.View
{
    public partial class FormKhachHang : Form
    {
        KhachhangAccess khacess = new KhachhangAccess();
        DataTable dskh = new DataTable();

        public FormKhachHang()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát không? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            KhachHang kh = new KhachHang(txtMakhachhang.Text, txtHodemkh.Text, txtTenkh.Text, txtgt.Text, txtDiachi.Text, txtSdt.Text, txtEmail.Text);
            if (khacess.suakh(kh) == 0) { MessageBox.Show("khách hàng  đã tồn tại !"); }
            else
            {
                MessageBox.Show("sửa thành công !");
            }
            FormKhachHang_Load(sender, e);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            KhachHang kh = new KhachHang(txtMakhachhang.Text, txtHodemkh.Text, txtTenkh.Text, txtgt.Text, txtDiachi.Text, txtSdt.Text, txtEmail.Text);
            if (khacess.xoakh(kh) == 0) { MessageBox.Show("khách hàng  đã tồn tại !"); }
            else
            {
                MessageBox.Show("xóa thành công !");
            }
            FormKhachHang_Load(sender, e);
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            KhachHang kh= new KhachHang(txtMakhachhang.Text,txtHodemkh.Text,txtTenkh.Text, txtgt.Text, txtDiachi.Text, txtSdt.Text, txtEmail.Text);
            if (khacess.themkh(kh)==0) { MessageBox.Show("khách hàng  đã tồn tại !"); }
            else
            {
                MessageBox.Show("Thêm thành công !");
            }
            FormKhachHang_Load(sender, e);
        }

     
        

        private void FormKhachHang_Load(object sender, EventArgs e)
        {
            dskh = khacess.Laytoanbokh();
            dataGridView1.DataSource = dskh;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMakhachhang.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            txtHodemkh.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            txtTenkh.Text= dataGridView1.CurrentRow.Cells[2].Value.ToString();
            txtgt.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            txtDiachi.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            txtSdt.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
            txtEmail.Text = dataGridView1.CurrentRow.Cells[6].Value.ToString();
        }

        private void chbMakhachhang_CheckedChanged(object sender, EventArgs e)
        {
            if (chbMakhachhang.Checked==true)
            {
                txtMakhachhang.ReadOnly = false;
                btnThem.Enabled = true;
            }
            else
            {
                txtMakhachhang.ReadOnly = false;
                btnThem.Enabled = false;
            }
        }
    }
}
